﻿
@{
    ViewBag.Title = "InfoView";
    Layout = "~/Views/Shared/_MyLayout.cshtml";
}
@section head{
    <script src="~/Scripts/exportJs.js"></script>
}

<fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
    <legend>前期测算信息</legend>
</fieldset>
<div>
    <form class="layui-form" enctype="multipart/form-data">
        <div class="layui-form-item">
            <label class="layui-form-label">主项目选择</label>
            <div class="layui-input-inline" style="width:500px;">
                <select name="ProjectId" lay-verify="required" lay-search lay-filter="ProjectSelFilter" id="ProjectSel">
                    <option value=""></option>
                    @for (int i = 0; i < ViewBag.ProjectList.Count; i++)
                    {
                        <option value="@ViewBag.ProjectList[i]["ProjectId"]">@ViewBag.ProjectList[i]["Name"]|@ViewBag.ProjectList[i]["ProjectCode"]|@ViewBag.ProjectList[i]["Type"]</option>
                    }
                </select>
            </div>
            <button type="button" class="layui-btn layui-btn-danger" style="float:left;" id="BtnAdd"><i class="layui-icon">&#xe61f;</i>新增</button>
        </div>
    </form>
</div>
<div id="DivAdd" style="display:none;margin-top:5px;">
    <form class="layui-form">
        <div class="layui-form-item">
            <label class="layui-form-label">新增报价测算表</label>
            <div class="layui-input-block">
                <input type="text" id="InputDivName" class="layui-input" placeholder="输入报价测算表名称" style="width:300px;float:left;">
                <button type="button" class="layui-btn layui-btn-danger" style="float:left;" id="BtnDivAdd">确认新增</button>
            </div>
        </div>
    </form>
</div>

<div id="DivAddCS" style="display:none;margin-top:5px;">
    <form class="layui-form">
        <div class="layui-form-item">
            <label class="layui-form-label">新增前期测算表</label>
            <div class="layui-input-block">
                <input type="text" id="InputDivCSName" class="layui-input" placeholder="输入报价测算表名称" style="width:300px;float:left;">
                <button type="button" class="layui-btn layui-btn-danger" style="float:left;" id="BtnDivAddCS">确认新增</button>
            </div>
        </div>
    </form>
    <table id="QuoteTable" lay-filter="QuoteTableFilter"></table>
</div>


<div id="DivBJCopy" style="display:none;margin-top:5px;">
    <form class="layui-form">
        <div class="layui-form-item">
            <label class="layui-form-label">复制报价测算表名字</label>
            <div class="layui-input-block">
                <input type="text" id="InputBJCopyName" class="layui-input" placeholder="输入报价测算表名称" style="width:300px;float:left;">
                <button type="button" class="layui-btn layui-btn-danger" style="float:left;" id="BtnBJCopyComfirm">确认新增</button>
            </div>
        </div>
    </form>
</div>

<div id="DivCSCopy" style="display:none;margin-top:5px;">
    <form class="layui-form">
        <div class="layui-form-item">
            <label class="layui-form-label">复制前期测算表名字</label>
            <div class="layui-input-block">
                <input type="text" id="InputCSopyName" class="layui-input" placeholder="输入报价测算表名称" style="width:300px;float:left;">
                <button type="button" class="layui-btn layui-btn-danger" style="float:left;" id="BtnCSCopyComfirm">确认新增</button>
            </div>
        </div>
    </form>
</div>


<div class="layui-tab" lay-filter="tabFilter">
    <ul class="layui-tab-title">
        <li class="layui-this">报价测算表</li>
        <li>前期测算表</li>
    </ul>
    <div class="layui-tab-content">
        <div class="layui-tab-item layui-show">
            <table id="demo" lay-filter="SubConTableList"></table>
        </div>
        <div class="layui-tab-item">
            <table id="CSTable" lay-filter="CSTableFilter"></table>
        </div>
    </div>
</div>




<script type="text/template" id="LayerProgBar">
    <div class="layui-progress layui-progress-big" lay-showPercent="true" lay-filter="progressBar">
        <div class="layui-progress-bar layui-bg-blue" lay-percent="0%"></div>
    </div>
</script>

<script type="text/javascript">
    var table;
    var TabIndex = 0;
    layui.use(['form', 'table', 'upload', 'layer', 'element'], function () {
        var element = layui.element;
        var form = layui.form;
        table = layui.table;
        var tab = parent.tab;
        var layer = layui.layer;
        var index;
        var layerIndex;
        var layerDivBJCopyIndex;
        var layerDivCSCopyIndex;
        var ProjectType = "";
        var ProjectName = "";
        var ProjectId = "";
        var ProjectCode = "";
        var tablePreEstId;

        //监听选项卡切换
        element.on('tab(tabFilter)', function (data) {
            //index从0开始
            TabIndex = data.index;
        });

        //新增弹出输入名字对话框
        $("#BtnAdd").on("click", function () {
            if (ProjectId == "") {
                return;
            }
            if (TabIndex == 0) {
                layerIndex = layer.open({
                    type: 1,
                    area: ['550px', '110px'],
                    content: $("#DivAdd"), //这里content是一个DOM，注意：最好该元素要存放在body最外层，否则可能被其它的相对元素所影响
                });
            }
            if (TabIndex == 1) {
                layerIndex = layer.open({
                    type: 1,
                    area: ['800px', '600px'],
                    content: $("#DivAddCS"), //这里content是一个DOM，注意：最好该元素要存放在body最外层，否则可能被其它的相对元素所影响
                });
            }

        });

        //报价测算 新增名字确认
        $("#BtnDivAdd").on("click", function () {
            var tempname = $("#InputDivName").val();
            tempname = tempname.replace(/\s*/g, "");
            if (tempname == "") {
                return;
            }
            $.ajax({
                type: "POST",
                url: "/Estimate/Add",
                async: false,
                data: { "ProjectType": ProjectType, "ProjectId": ProjectId, "Name": tempname },
                success: function (msg) {
                    if (msg.result == "OK") {
                        layer.msg('添加成功"', { icon: 6, time: 1000 });
                        table.reload("demo", {
                            //where: { SortName: "SSlistId", Order: "Desc", Condition: " and 1=1" }
                        });
                        layer.close(layerIndex);
                    } else {
                        alert(msg.result);
                    }
                }
            });
        });
        //前期测算  引用报价测算表格
        table.render({
            elem: '#QuoteTable'
            , method: 'post'
            //, height: 315
            , url: '/Estimate/EstimateViewTableRequest' //数据接口
            , page: true //开启分页
            , where: { SortName: "PreEstId", Order: "Desc", Condition: " and 1=0" }
            , limits: [10, 50, 100]
            , cols: [[ //表头
                { type: 'radio' }
                , { field: 'Name', title: '引入报价测算表名称', width: 250 }
            ]]
        });

        //前期测算  新增名字确认
        $("#BtnDivAddCS").on("click", function () {
            var tempname = $("#InputDivCSName").val();
            tempname = tempname.replace(/\s*/g, "");
            if (tempname == "") {
                return;
            }
            var checkStatus = table.checkStatus('QuoteTable');
            var data = checkStatus.data;    //这里得到的data是array类型
            if (data.length != 1) {
                layer.msg('请选中一行', { icon: 3, time: 1000 });
                return;
            }

            $.ajax({
                type: "POST",
                url: "/Estimate/AddEstUserCS",
                async: false,
                data: { "PreEstUserId": data[0].PreEstId, "ProjectId": data[0].ProjectId, "Name": tempname },
                success: function (msg) {
                    if (msg.result == "OK") {
                        layer.msg('添加成功"', { icon: 6, time: 1000 });
                        table.reload("CSTable", {
                            //where: { SortName: "SSlistId", Order: "Desc", Condition: " and 1=1" }
                        });
                        layer.close(layerIndex);
                    } else {
                        alert(msg.result);
                    }
                }
            });
        });
        ////////////////////////////////////////////////////////////////////
        //复制确认按钮 报价测算表
        $("#BtnBJCopyComfirm").on("click", function () {
            var tempname = $("#InputBJCopyName").val();
            tempname = tempname.replace(/\s*/g, "");
            if (tempname == "") {
                return;
            }
            $.ajax({
                type: "POST",
                url: "/Estimate/EstUserCopy",
                async: false,
                data: { "PreEstId": tablePreEstId, "ProjectId": ProjectId, "Name": tempname },
                success: function (msg) {
                    if (msg.result == "OK") {
                        layer.msg('添加成功"', { icon: 6, time: 1000 });
                        table.reload("demo", {
                            //where: { SortName: "SSlistId", Order: "Desc", Condition: " and 1=1" }
                        });
                        layer.close(layerDivBJCopyIndex);
                    } else {
                        alert(msg.result);
                    }
                }
            });
        });
        //复制确认按钮 前期测算表
        $("#BtnCSCopyComfirm").on("click", function () {
            var tempname = $("#InputCSopyName").val();
            tempname = tempname.replace(/\s*/g, "");
            if (tempname == "") {
                return;
            }
            $.ajax({
                type: "POST",
                url: "/Estimate/EstUserCSCopy",
                async: false,
                data: { "PreEstId": tablePreEstId, "ProjectId": ProjectId, "Name": tempname },
                success: function (msg) {
                    if (msg.result == "OK") {
                        layer.msg('添加成功"', { icon: 6, time: 1000 });
                        table.reload("CSTable", {
                            //where: { SortName: "SSlistId", Order: "Desc", Condition: " and 1=1" }
                        });
                        layer.close(layerDivCSCopyIndex);
                    } else {
                        alert(msg.result);
                    }
                }
            });
        });


        form.on('select(ProjectSelFilter)', function (data) {
            //console.log(data.elem); //得到select原始DOM对象
            //console.log(data.value); //得到被选中的值
            //console.log(data.othis); //得到美化后的DOM对象

            ProjectId = data.value;
            if (data.value == "") {
                return;
            }
            //下面需要先查工程类型           
            $.ajax({
                type: "POST",
                url: "/Project/QueryProjectInfo",
                async: false,
                data: { "ProjectId": data.value },
                success: function (msg) {
                    if (msg.result == "OK") {
                        table.reload("demo", {
                            where: {
                                SortName: "PreEstId", Order: "Desc", Condition: " and IsDelete<>1 and Type='" + msg.type + "' and ProjectId=" + data.value
                            }                          
                        });
                        table.reload("QuoteTable", {
                            where: {
                                SortName: "PreEstId", Order: "Desc", Condition: " and IsDelete<>1 and Type='" + msg.type + "' and ProjectId=" + data.value
                            }
                        });
                        table.reload("CSTable", {
                            where: {
                                SortName: "PreEstId", Order: "Desc", Condition: " and IsDelete<>1 and Type='用户工程前期测算' and ProjectId=" + data.value
                            }
                        });
                        ProjectType = msg.type;
                        ProjectName = msg.Name;
                    } else {
                        alert(msg.result);
                        return;
                    }
                }
            });
        });

        ///////////////////////////////////////////////////////////////////////
        //报价测算
        table.render({
            elem: '#demo'
            , method: 'post'
            //, height: 315
            , url: '/Estimate/EstimateViewTableRequest' //数据接口
            , page: true //开启分页
            , where: { SortName: "PreEstId", Order: "Desc", Condition: " and 1=0" }
            , limits: [10, 50, 100]
            , cols: [[ //表头
                { field: 'Name', title: '前期测算表名称', width: 250, edit: 'text', }
                , {
                    field: "operation", title: "操作", width: 200, fixed: 'right',
                    templet: function (d) {
                        return '<a class="layui-btn layui-btn-xs" lay-event="detail">详情</a><a class="layui-btn layui-btn-xs" lay-event="TableBJCopy">复制</a>'+
                            '<a class="layui-btn layui-btn-xs" lay-event="Export">导出</a><a class="layui-btn layui-btn-xs" lay-event="TableDel">删除</a>';
                    }
                }
            ]]
        });


        //鼠标双击事件回调
        table.on('rowDouble(SubConTableList)', function (obj) {
            var data = obj.data;

        });

        table.on("tool(SubConTableList)", function (obj) {
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
            var tr = obj.tr; //获得当前行 tr 的DOM对象

            if (layEvent == 'detail') {
                //parent.layui.index.openTabsPage('/SubContract/SubConInfoEditView?SubContractId=' + data.SubContractId, '分包合同编辑');
                var type = $("#ProjectSel").find("option:selected").text();


                var tempForm = document.createElement("form");
                tempForm.id = "tempForm1";
                tempForm.method = "post";
                tempForm.action = "/Estimate/EditIndex";
                tempForm.target = "_blank"; //打开新页面

                var hideInput1 = document.createElement("input");
                hideInput1.type = "hidden";
                hideInput1.name = "PreEstId"; 
                hideInput1.value = data.PreEstId; 
                var hideInput2 = document.createElement("input");
                hideInput2.type = "hidden";
                hideInput2.name ="ProjectName";
                hideInput2.value = ProjectName;
                var hideInput3 = document.createElement("input");
                hideInput3.type = "hidden";
                hideInput3.name = "ProjectType";
                hideInput3.value = ProjectType;
                var hideInput4 = document.createElement("input");
                hideInput4.type = "hidden";
                hideInput4.name = "PreEstName";
                hideInput4.value = data.Name;

                tempForm.appendChild(hideInput1);
                tempForm.appendChild(hideInput2);
                tempForm.appendChild(hideInput3);
                tempForm.appendChild(hideInput4);

                if (document.all) {
                    tempForm.attachEvent("onsubmit", function () { });        //IE
                } else {
                    var subObj = tempForm.addEventListener("submit", function () { }, false);    //firefox
                }
                document.body.appendChild(tempForm);
                if (document.all) {
                    tempForm.fireEvent("onsubmit");
                } else {
                    tempForm.dispatchEvent(new Event("submit"));
                }
                tempForm.submit();
                document.body.removeChild(tempForm);



            }

            if (layEvent == 'TableBJCopy') {
                tablePreEstId = data.PreEstId; 
                layerDivBJCopyIndex = layer.open({
                    type: 1,
                    area: ['550px', '110px'],
                    content: $("#DivBJCopy"), //这里content是一个DOM，注意：最好该元素要存放在body最外层，否则可能被其它的相对元素所影响
                });
            }

            if (layEvent == 'Export') {

                var formstr = "PreEstId::" + data.PreEstId + "&Name::" + data.Name + "&ProjectName::" + ProjectName;
                $.down('/Estimate/EstUserExport', formstr, 'post');
            }

            if (layEvent == 'TableDel') {
                layer.confirm('确实删除：' + data.Name , { icon: 3, title: '提示' }, function (index) {
                    //do something
                    $.ajax({
                        type: "POST",
                        url: "/Estimate/EstUserDel",
                        async: false,
                        data: { "PreEstId": data.PreEstId },
                        success: function (msg) {
                            if (msg.result == "OK") {
                                parent.layer.msg('删除成功', { icon: 6, time: 1000 });
                                table.reload("demo");
                            } else {
                                alert(msg.result);
                            }
                        }
                    });
                    layer.close(index);
                });
            }

        });

        table.on('edit(SubConTableList)', function (obj) {
            //console.log(obj.value); //得到修改后的值
            //console.log(obj.field); //当前编辑的字段名
            //console.log(obj.data); //所在行的所有相关数据  
            var data = obj.data;
            var field = obj.field;

            //下面更新数据
            $.ajax({
                type: "POST",
                url: "/Estimate/EstUserNameUpdate",
                async: false,
                data: { "PreEstId": data.PreEstId, "Name": data.Name },
                success: function (msg) {
                    if (msg.result == "OK") {
                        parent.layer.msg('修改成功', { icon: 6, time: 1000 });
                        table.reload('demo');

                    } else {
                        alert(msg.result);
                    }
                }
            });
        });

        ///////////////////////////////////////////////////
        //前期测算
        table.render({
            elem: '#CSTable'
            , method: 'post'
            //, height: 315
            , url: '/Estimate/EstUserCSTableRequest' //数据接口
            , page: true //开启分页
            , where: { SortName: "PreEstId", Order: "Desc", Condition: " and 1=0" }
            , limits: [10, 50, 100]
            , cols: [[ //表头
                { field: 'Name', title: '前期测算表名称', width: 250, edit: 'text', }
                , {
                    field: "operation", title: "操作", width: 300, fixed: 'right',
                    templet: function (d) {
                        return '<a class="layui-btn layui-btn-xs" lay-event="detail">详情</a><a class="layui-btn layui-btn-xs" lay-event="TableCSCopy">复制</a>' +
                            '<a class="layui-btn layui-btn-xs" lay-event="Export">导出</a><a class="layui-btn layui-btn-xs" lay-event="TableDel">删除</a>' +
                            '<a class="layui-btn layui-btn-xs" lay-event="WorkQuantities">工程量表</a>';
                    }
                }
            ]]
        });


        table.on('edit(CSTableFilter)', function (obj) {
            //console.log(obj.value); //得到修改后的值
            //console.log(obj.field); //当前编辑的字段名
            //console.log(obj.data); //所在行的所有相关数据  
            var data = obj.data;
            var field = obj.field;

            //下面更新数据
            $.ajax({
                type: "POST",
                url: "/Estimate/EstUserCSNameUpdate",
                async: false,
                data: { "PreEstId": data.PreEstId, "Name": data.Name },
                success: function (msg) {
                    if (msg.result == "OK") {
                        parent.layer.msg('修改成功', { icon: 6, time: 1000 });
                        table.reload('CSTable');

                    } else {
                        alert(msg.result);
                    }
                }
            });
        });


        table.on("tool(CSTableFilter)", function (obj) {
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
            var tr = obj.tr; //获得当前行 tr 的DOM对象

            if (layEvent == 'detail') {
                //parent.layui.index.openTabsPage('/SubContract/SubConInfoEditView?SubContractId=' + data.SubContractId, '分包合同编辑');
                var type = $("#ProjectSel").find("option:selected").text();


                var tempForm = document.createElement("form");
                tempForm.id = "tempForm1";
                tempForm.method = "post";
                tempForm.action = "/Estimate/EditIndex";
                tempForm.target = "_blank"; //打开新页面

                var hideInput1 = document.createElement("input");
                hideInput1.type = "hidden";
                hideInput1.name = "PreEstId";
                hideInput1.value = data.PreEstId;
                var hideInput2 = document.createElement("input");
                hideInput2.type = "hidden";
                hideInput2.name = "ProjectName";
                hideInput2.value = ProjectName;
                var hideInput3 = document.createElement("input");
                hideInput3.type = "hidden";
                hideInput3.name = "ProjectType";
                hideInput3.value = "用户工程前期测算";
                var hideInput4 = document.createElement("input");
                hideInput4.type = "hidden";
                hideInput4.name = "PreEstName";
                hideInput4.value = data.Name;

                tempForm.appendChild(hideInput1);
                tempForm.appendChild(hideInput2);
                tempForm.appendChild(hideInput3);
                tempForm.appendChild(hideInput4);

                if (document.all) {
                    tempForm.attachEvent("onsubmit", function () { });        //IE
                } else {
                    var subObj = tempForm.addEventListener("submit", function () { }, false);    //firefox
                }
                document.body.appendChild(tempForm);
                if (document.all) {
                    tempForm.fireEvent("onsubmit");
                } else {
                    tempForm.dispatchEvent(new Event("submit"));
                }
                tempForm.submit();
                document.body.removeChild(tempForm);



            }

            if (layEvent == 'TableCSCopy') {
                tablePreEstId = data.PreEstId;
                layerDivCSCopyIndex = layer.open({
                    type: 1,
                    area: ['550px', '110px'],
                    content: $("#DivCSCopy"), //这里content是一个DOM，注意：最好该元素要存放在body最外层，否则可能被其它的相对元素所影响
                });
            }

            if (layEvent == 'Export') {

                var formstr = "PreEstId::" + data.PreEstId + "&Name::" + data.Name + "&ProjectName::" + ProjectName;
                $.down('/Estimate/EstUserCSExport', formstr, 'post');
            }

            if (layEvent == 'TableDel') {
                layer.confirm('确实删除：' + data.Name, { icon: 3, title: '提示' }, function (index) {
                    //do something
                    $.ajax({
                        type: "POST",
                        url: "/Estimate/EstUserCSDel",
                        async: false,
                        data: { "PreEstId": data.PreEstId },
                        success: function (msg) {
                            if (msg.result == "OK") {
                                parent.layer.msg('删除成功', { icon: 6, time: 1000 });
                                table.reload("CSTable");
                            } else {
                                alert(msg.result);
                            }
                        }
                    });
                    layer.close(index);
                });
            }

            if (layEvent == 'WorkQuantities') {
                window.open("/Estimate/WorkQuantitiesView?PreEstId=" + data.PreEstId + "&ProjectId=" + ProjectId);
            }

        });

    });

    //回车键
    function getKey() {
        if (event.keyCode == 13) {
            SearchFunc();
        }
    }

    function SearchFunc() {
        var text = $("#SearchText").val();
        if (text == "") {
            table.reload("demo", {
                where: {
                    SortName: "SubContractId", Order: "Desc", Condition: " and a.IsDelete<>1"
                }
            });
        }
        else {
            table.reload("demo", {
                where: {
                    SortName: "SubContractId", Order: "Desc", Condition: " and a.IsDelete<>1 and (Name like '%" + text + "%' or HTMC like '%" + text + "%')"
                },
                page: {
                    curr: 1
                }
            });
        }
    }



    function dealNumber(money) {
        if (money && money != null) {
            money = String(money);
            var left = money.split('.')[0], right = money.split('.')[1];
            right = right ? (right.length >= 2 ? '.' + right.substr(0, 2) : '.' + right + '0') : '.00';
            var temp = left.split('').reverse().join('').match(/(\d{1,3})/g);
            return (Number(money) < 0 ? "-" : "") + temp.join(',').split('').reverse().join('') + right;
        } else if (money === 0) {   //注意===在这里的使用，如果传入的money为0,if中会将其判定为boolean类型，故而要另外做===判断
            return '0.00';
        } else {
            return "";
        }
    };

    function openWindow(name) {
        window.open('about:blank',name);
    }    


</script>

